package com.waterworld.watch.chat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.waterworld.watch.R;
import com.waterworld.watch.chat.adapter.CreateGroupAdapter;
import com.waterworld.watch.chat.bean.MemberSelectBean;
import com.waterworld.watch.chat.interfaces.IChatManager;
import com.waterworld.watch.chat.manager.ChatManager;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.GroupAllowUserBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.activity.HomePagerActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateGroupActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_confirm)
    TextView header_confirm;

    @BindView(R.id.tv_can_select)
    TextView tv_can_select;

    @BindView(R.id.et_group_name)
    EditText et_group_name;

    @BindView(R.id.rv_member)
    RecyclerView rv_member;

    private List<MemberSelectBean> mList = new ArrayList<>();
    private CreateGroupAdapter mAdapter;
    private IChatManager iChatManager = ChatManager.getInstance();
    private UserInfoBean.WatchUser watchUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_group);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        setViewSize(header_parent, LinearLayout.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setText(getString(R.string.select_member));
        header_title.setVisibility(View.VISIBLE);
        header_confirm.setVisibility(View.VISIBLE);
        header_confirm.setText(getString(R.string.confirm));

        watchUser = (UserInfoBean.WatchUser) getIntent().getSerializableExtra("watch");
        tv_can_select.setText(String.format(getString(R.string.member_can_select), watchUser.getName()));
        if (iChatManager != null) {
            iChatManager.listGroupAllowUser(watchUser.getWatchId(), new BaseObserverListener<BaseResultBean<GroupAllowUserBean[]>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(BaseResultBean<GroupAllowUserBean[]> baseResultBean) {
                    for (GroupAllowUserBean bean : baseResultBean.getData()) {
                        mList.add(new MemberSelectBean(bean,false));
                    }
                    mAdapter = new CreateGroupAdapter(CreateGroupActivity.this,mList);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CreateGroupActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv_member.setLayoutManager(linearLayoutManager);
                    rv_member.setAdapter(mAdapter);
                }
            });
        }
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }

    @OnClick(R.id.header_confirm)
    void onCreateGroup() {
        List<MemberSelectBean> list = new ArrayList<>();
        for (MemberSelectBean bean : mAdapter.getData()) {
            if (bean.getSelect()) {
                list.add(bean);
            }
        }
        if (list.size() == 0) {
            ToastUtils.showShort(CreateGroupActivity.this,"必须要选择一个用户！");
        } else if (TextUtils.isEmpty(et_group_name.getText().toString().trim())) {
            ToastUtils.showShort(CreateGroupActivity.this,"必须要填写群名称！");
        } else {
            if (iChatManager != null) {
                Map<String,Object> map = new HashMap<>();
                map.put("name",et_group_name.getText().toString().trim());
                map.put("head",null);
                map.put("watchId",watchUser.getWatchId());
                List<Map<String,Object>> member = new ArrayList<>();
                for (MemberSelectBean bean : list) {
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("memberType",bean.getGroupAllowUserBean().getUserType());
                    map1.put("memberId",bean.getGroupAllowUserBean().getUserId());
                    map1.put("memberName",bean.getGroupAllowUserBean().getName());
                    member.add(map1);
                }

                map.put("groupMember",member);
                String json = JSONObject.toJSONString(map);
                iChatManager.createGroup(json, new BaseObserverListener<BaseResultBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BaseResultBean baseResultBean) {
                        AppManager.finishActivity(CreateGroupActivity.class);
                        AppManager.finishActivity(SelectWatchActivity.class);
                    }
                });
            }
        }
    }
}
